package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.alipay.PayActivity;
import edu.sctu.yueding.bean.OrderInfoBean;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;

/**
 * Created by zcy on 2016/8/20.
 */
public class MyOrderAdapter extends BaseAdapter {

    private Activity activity;
    private ViewHolder viewHolder;
    List<OrderInfoBean> data;
    private int flag;


    public MyOrderAdapter(Activity activity, List<OrderInfoBean> data, int flag) {
        this.activity = activity;
        this.data = data;
        this.flag = flag;
    }


    /**
     * 刷新数据
     *
     * @param data
     */
    public void fresh(List<OrderInfoBean> data, boolean isAddAll) {

        if (isAddAll) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        if (data != null && data.size() > 0) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {

        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {

        View view;

        if (convertView == null) {
            view = LayoutInflater.from(activity).inflate(R.layout.item_my_order, null);
            viewHolder = new ViewHolder();

            viewHolder.orderId = (TextView) view.findViewById(R.id.order_id);
            viewHolder.orderTitle = (TextView) view.findViewById(R.id.my_order_title);
            viewHolder.orderRoute = (TextView) view.findViewById(R.id.my_order_route);
            viewHolder.orderMoney = (TextView) view.findViewById(R.id.my_order_money);
//            viewHolder.orderCancel = (TextView) view.findViewById(R.id.my_order_cancel);
            viewHolder.OrderPay = (TextView) view.findViewById(R.id.my_order_pay);
            viewHolder.orderPic = (ImageView) view.findViewById(R.id.my_order_pic);
            view.setTag(viewHolder);//将viewHolder储存在view中

        } else {

            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        if (data != null && !data.toString().equals("[]")) {

            viewHolder.orderId.setText("订单号:" + data.get(i).getOrderNo());
            viewHolder.orderTitle.setText(data.get(i).getOrderName());
            viewHolder.orderMoney.setText("￥" + data.get(i).getTotalAmount());

            if (data.get(i).getOrderDescription() != null) {

                GetVolleyHttp.getInstance(activity).setImageLoader(viewHolder.orderPic,
                        R.drawable.default_ptr_rotate, R.drawable.default_ptr_rotate,
                        data.get(i).getOrderDescription());
            }


            if (flag == 1) {

//                viewHolder.orderCancel.setBackground(view.getResources().getDrawable(R.drawable.bg_round_my_oeder_cancel));
//                viewHolder.orderCancel.setText("删除订单");
//                viewHolder.orderCancel.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//
//                        GetVolleyHttp.getInstance(activity).getStringGETJsonData(URLConfig.getDeleteOrder(data.get(i).getId()), new Response.Listener<String>() {
//                            @Override
//                            public void onResponse(String response) {
//
//                                Toast.makeText(activity, "成功删除", Toast.LENGTH_SHORT).show();
//
//                            }
//                        }, new Response.ErrorListener() {
//                            @Override
//                            public void onErrorResponse(VolleyError error) {
//                                Toast.makeText(activity, "网络出了点问题！", Toast.LENGTH_SHORT).show();
//
//                            }
//                        }, "");
//                    }
//                });


                viewHolder.OrderPay.setBackground(view.getResources().getDrawable(R.drawable.bg_round_topic_color));
                viewHolder.OrderPay.setText("确认付款");


                viewHolder.OrderPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Bundle bundle = new Bundle();

                        bundle.putBoolean("payAgain",true);
                        bundle.putLong("matchId",data.get(i).getMatchId());
                        bundle.putLong("teamId",data.get(i).getTeamId());
                        bundle.putLong("routeId",data.get(i).getRouteId());

                        JumpUtils.jumpActivity(activity, PayActivity.class, bundle);
                    }
                });


            } else if (flag == 0) {

                viewHolder.OrderPay.setBackground(view.getResources().getDrawable(R.drawable.bg_round_my_oeder_finished));
                viewHolder.OrderPay.setText("已完成");
            }


        }

        return view;

    }

    private class ViewHolder {

        TextView orderId, orderTitle, orderRoute, orderMoney, orderCancel, OrderPay;
        ImageView orderPic;

    }
}
